package com.ulez.scaleview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by ulez on 2016/7/18.
 */
public class ScaleView extends View {
    private String TAG = "ScaleView";
    private RectF rectF;
    private Paint paint_gray2;
    private Paint paint_gray;
    private Paint paint_green;
    private int mWidth;
    private int mHeight;
    private String showText = "Download?";
    private int innerTextNormalColor=Color.WHITE;
    private int innerTextFailColor=Color.BLACK;
    private int innerCircleColor=Color.BLACK;
    private int outArcColor=Color.BLACK;
    private float innerTextSize=15.0f;
    private float outArcWidthSize=15.0f;

    private float paddingWidthSize;


    public ScaleView(Context context) {
        super(context);
        init(context, null);
    }

    public ScaleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ScaleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


//    <attr name="innerTextNormalColor" format="color"></attr>
//    <attr name="innerTextFailColor" format="color"></attr>
//    <attr name="innerCircleColor" format="color"></attr>
//    <attr name="outArcColor" format="color"></attr>
//    <attr name="innerTextSize" format="dimension"></attr>

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.ScaleView);
        if (ta!=null){
            innerTextNormalColor= ta.getColor(R.styleable.ScaleView_innerTextNormalColor,Color.WHITE);
            innerTextFailColor=ta.getColor(R.styleable.ScaleView_innerTextFailColor,Color.BLACK);
            innerCircleColor= ta.getColor(R.styleable.ScaleView_innerCircleColor,Color.BLACK);
            outArcColor=ta.getColor(R.styleable.ScaleView_outArcColor,Color.BLACK);
            innerTextSize = ta.getDimension(R.styleable.ScaleView_innerTextSize,15);
            outArcWidthSize = ta.getDimension(R.styleable.ScaleView_outArcWidthSize,15);
            paddingWidthSize = ta.getDimension(R.styleable.ScaleView_paddingWidthSize,50);
        }

        paint_gray2 = new Paint();
        paint_gray2.setColor(innerCircleColor);

        paint_green = new Paint();
        paint_green.setColor(innerTextNormalColor);
        paint_green.setTextSize(innerTextSize);

        paint_gray = new Paint();
        paint_gray.setColor(outArcColor);
        paint_gray.setAntiAlias(true);
        paint_gray.setStyle(Paint.Style.STROKE);
        paint_gray.setStrokeWidth(outArcWidthSize);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    int progress = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mWidth / 2, mWidth / 2, Math.min(mWidth / 2, mHeight / 2) / 2, paint_gray2);
        canvas.drawArc(rectF, 270, progress, false, paint_gray);



        Rect bounds1 = new Rect();
        paint_green.getTextBounds(showText, 0, showText.length(), bounds1);

        Paint.FontMetricsInt fontMetrics = paint_green.getFontMetricsInt();
        int baseline = (mHeight - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
        canvas.drawText(showText, 0, showText.length(), (mWidth -bounds1.width())/2,baseline, paint_green);

        Log.e(TAG,progress+showText);

        if (progress >= 360) {
            showText = "Finished";
            canvas.drawCircle(mWidth / 2, mWidth / 2, Math.min(mWidth / 2, mHeight / 2) / 2, paint_gray2);
            canvas.drawArc(rectF, 270, progress += 50, false, paint_gray);
            Rect bounds2 = new Rect();
            paint_green.getTextBounds(showText, 0, showText.length(), bounds2);
            Paint.FontMetricsInt fontMetrics2 = paint_green.getFontMetricsInt();
            int baseline2 = (mHeight - fontMetrics2.bottom + fontMetrics2.top) / 2 - fontMetrics2.top;
            canvas.drawText(showText, 0, showText.length(),  (mWidth -bounds2.width())/2, baseline2, paint_green);
        }
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        rectF = new RectF(paddingWidthSize, paddingWidthSize, mWidth - paddingWidthSize, mHeight - paddingWidthSize);
    }

    public void setProgress(int progress) {
        if (progress<0){
            this.progress = 0;
            this.showText = "Failed!";
            postInvalidate();
            return;
        }
        this.progress = progress;
        this.showText = (100*progress/360)+"%";
        postInvalidate();
    }
}
